/*
 * Copyright (C) 2016 volders GmbH with <3 in Berlin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package berlin.volders.badger;

import ohos.agp.components.Component;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.ColorFilter;
import ohos.media.image.common.PixelFormat;

/**
 * Base for all badge drawables. Implementing the abstract methods {@link #setAlpha(int)} and
 * {@link #setColorFilter(ColorFilter)} and handling compatibility for {@link #getAlpha()} and
 * {@link #getColorFilter()}.
 */
public abstract class BadgeDrawable extends ShapeElement implements Component.DrawTask {

    public static final int NUM_255 = 255;
    private int alpha = NUM_255;
    private ColorFilter colorFilter;

    /**
     * 设置透明度
     *
     * @param alpha
     */
    @Override
    public void setAlpha(@IntRange(from = 0, to = NUM_255) int alpha) {
        if (alpha > NUM_255) {
            alpha = NUM_255;
        } else if (alpha < 0) {
            alpha = 0;
        }
        if (this.alpha != alpha) {
            this.alpha = alpha;
//            invalidateSelf();
            clearColorMatrix();
        }
    }

    /**
     * 获取透明度
     *
     * @return int
     */
    @Override
    public int getAlpha() {
        return alpha;
    }

    /**
     * 设置颜色过滤器
     *
     * @param colorFilter
     */
    public void setColorFilter(ColorFilter colorFilter) {
        if (this.colorFilter != colorFilter) {
            this.colorFilter = colorFilter;
//            invalidateSelf();
            clearColorMatrix();
        }
    }

    /**
     * 获取颜色过滤器
     *
     * @return ColorFilter
     */
    public ColorFilter getColorFilter() {
        return colorFilter;
    }

    /**
     * 获取像素格式
     *
     * @return PixelFormat
     */
    public PixelFormat getOpacity() {
        if (alpha == NUM_255) {
//            return PixelFormat.OPAQUE;
        }
        if (alpha == 0) {
//            return PixelFormat.TRANSPARENT;
        }
//        return PixelFormat.TRANSLUCENT;
        return PixelFormat.ARGB_8888;
    }

    /**
     * {@code Factory} to create new badges when needed with
     * {@link Badger#sett(Element, BadgeDrawable.Factory)}.
     *
     * @param <T> the subtype of {@code BadgeDrawable} to create
     */
    public interface Factory<T extends BadgeDrawable> {
        T createBadge();
    }

    /**
     * 获取状态
     *
     * @return ConstantState
     */
    public ConstantState getConstantState() {
        return new DummyState();
    }

    private class DummyState extends ConstantState {

        DummyState() {
        }

        public ShapeElement newDrawable() {
            return BadgeDrawable.this;
        }

        public int getChangingConfigurations() {
            return 0;
        }
    }
}
